/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.sd.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jeesite.common.lang.DateUtils;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.mybatis.mapper.query.QueryType;
import com.jeesite.common.utils.excel.ExcelExport;
import com.jeesite.modules.sd.common.constants.GlobalEnum;
import com.jeesite.modules.sd.entity.DocRecv;
import com.jeesite.modules.sd.service.DocRecvService;
import com.jeesite.modules.sys.entity.EmpUser;
import com.jeesite.modules.sys.entity.Office;
import com.jeesite.modules.sys.entity.User;
import com.jeesite.modules.sys.service.EmpUserService;
import com.jeesite.modules.sys.service.UserService;
import com.jeesite.modules.sys.utils.EmpUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.sd.entity.DocDeliverRecord;
import com.jeesite.modules.sd.service.DocDeliverRecordService;

import java.util.ArrayList;
import java.util.List;

/**
 * 文件复制Controller
 * 分发列表
 *
 * @author ysj
 * @version 2020-07-01
 */
@Controller
@RequestMapping(value = "${adminPath}/sd/docDeliverTake")
public class DocDeliverTakeController extends BaseController {

    @Autowired
    private DocDeliverRecordService recordService;

    @Autowired
    private DocRecvService docRecvService;
    @Autowired
    private EmpUserService empUserService;
    @Autowired
    private UserService userService;

    /**
     * 获取数据
     */
    @ModelAttribute
    public DocDeliverRecord get(String id, boolean isNewRecord) {
        return recordService.get(id, isNewRecord);
    }
    /**
     * 查询列表
     */
    @RequiresPermissions("sd:docDeliverTake:view")
    @RequestMapping(value = {"list", ""})
    public String list(DocDeliverRecord docDeliverRecord, Model model) {
        String str = docDeliverRecord.getRecvUnitCode();
        model.addAttribute("docDeliverRecord", docDeliverRecord);
        return "modules/sd/docDeliverTakeList";
    }

    @RequiresPermissions("sd:docDeliverTake:view")
    @RequestMapping(value = {"index", ""})
    public String index(DocDeliverRecord docDeliverRecord, Model model) {

        model.addAttribute("docDeliverRecord", docDeliverRecord);
        return "modules/sd/docDeliverTakeIndex";
    }

    /**
     * 查询分发文件列表数据
     */
    @RequiresPermissions("sd:docDeliverTake:view")
    @RequestMapping(value = "listData")
    @ResponseBody
    public Page<DocDeliverRecord> listData(DocDeliverRecord docDeliverRecord, HttpServletRequest request, HttpServletResponse response, String docId) {
        docDeliverRecord.setPage(new Page<>(request, response));
        docDeliverRecord.getSqlMap().getWhere().andBracket("deliver_type", QueryType.EQ, GlobalEnum.RECORD_DELIVER_TYPE_DELIVER.getKey(), 1).or("deliver_type", QueryType.EQ, GlobalEnum.RECORD_DELIVER_TYPE_SEND.getKey(), 2).endBracket();
        docDeliverRecord.getSqlMap().getWhere().and("destroy_task_id",QueryType.IS_NULL,null);
        if (StringUtils.isNotEmpty(docId)) {
            docDeliverRecord.setDocId(docId);
        }
        Page<DocDeliverRecord> page = recordService.findPage(docDeliverRecord);
        return page;
    }

    /**
     * 导出分发列表
     * @param
     * @param isAll
     * @param ctrlPermi
     * @param response
     */
    @RequestMapping({"exportData"})
    public void exportData(DocDeliverRecord docDeliverRecord, Boolean isAll, String ctrlPermi, HttpServletResponse response,HttpServletRequest request) {
       docDeliverRecord.getSqlMap().getWhere().andBracket("deliver_type",QueryType.EQ,GlobalEnum.RECORD_DELIVER_TYPE_DELIVER.getKey(),1).or("deliver_type",QueryType.EQ,GlobalEnum.RECORD_DELIVER_TYPE_SEND.getKey(),2).endBracket();
       docDeliverRecord.getSqlMap().getWhere().and("destroy_task_id",QueryType.IS_NULL,null);
        List<DocDeliverRecord> list = this.recordService.findList(docDeliverRecord);
        String fileName = "文件分发记录" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
        ExcelExport ee = new ExcelExport("文件分发数据", DocDeliverRecord.class);
        Throwable var8 = null;

        try {
            ee.setDataList(list).write(response, fileName);
        } catch (Throwable var17) {
            var8 = var17;
            throw var17;
        } finally {
            if (ee != null) {
                if (var8 != null) {
                    try {
                        ee.close();
                    } catch (Throwable var16) {
                        var8.addSuppressed(var16);
                    }
                } else {
                    ee.close();
                }
            }

        }

    }



    /**
     * 单个拟办文件领取 查询警号
     * @param userCode
     * @return
     */
    @RequiresPermissions("sd:docDeliverTake:edit")
    @RequestMapping(value = "selectUserInfor")
    @ResponseBody
    public String selectUserInfor(String userCode) {

        User user = userService.get(userCode);

        return user.getExtend().getExtendS1();
    }

    /**
     * 查询待领文件通过单位id/用户id
     *
     * @param id 用户id
     * @return offId 单位id
     */
    @RequestMapping(value = "selectFileUnit")
    @ResponseBody
    public String selectFileUnit(String offId) {
        if (StringUtils.isNotEmpty(offId)){
        List<DocDeliverRecord> list;
        DocDeliverRecord docDeliverRecord = new DocDeliverRecord();
        docDeliverRecord.getSqlMap().getWhere().and("take_date", QueryType.IS_NULL, null);
        docDeliverRecord.getSqlMap().getWhere().and("deliver_type", QueryType.IN, new String[]{"2", "3"});
        Office office = new Office();
        office.setOfficeCode(offId);
        docDeliverRecord.setOffice(office);
        list = recordService.findList(docDeliverRecord);

        if (CollectionUtils.isEmpty(list)) {
            return renderResult(Global.FALSE, text("该单位没有文件"));
        } else {
            return renderResult(Global.TRUE, text("请核对信息"), list);
        }
        }else{
            return "";
        }

    }

    /**
     * 查看编辑表单
     * @param docDeliverRecord
     * @param model
     * @return
     */
    @RequiresPermissions("sd:docDeliverTake:view")
    @RequestMapping(value = "form")
    public String form(DocDeliverRecord docDeliverRecord, Model model) {
        String str = docDeliverRecord.getDocRecv().getId();
        DocRecv docRecv = docRecvService.get(str);
        model.addAttribute("docRecv", docRecv);
        return "modules/sd/docDeliverHistoryShow";
    }

    /**
     * 查看编辑表单
     */
    @RequiresPermissions("sd:docDeliverTake:view")
    @RequestMapping(value = "checkFenFa")
    public String checkFenFa(DocDeliverRecord docDeliverRecord, Model model) {
        String str = docDeliverRecord.getDocRecv().getId();
        DocRecv docRecv = docRecvService.get(str);

        model.addAttribute("docRecv", docRecv);
        return "modules/sd/docDeliverTakeForm";
    }

    /**
     * 保存数据
     */
    @RequiresPermissions("sd:docDeliverTake:edit")
    @PostMapping(value = "save")
    @ResponseBody
    public String save(@Validated DocDeliverRecord docDeliverRecord) {
        recordService.save(docDeliverRecord);
        return renderResult(Global.TRUE, text("保存文件分发成功！"));
    }

    @RequiresPermissions("sd:docDeliverTake:edit")
    @RequestMapping("printCodeByRecordId")
    @ResponseBody
    public String printCodeByRecordId(DocDeliverRecord record) {
        //打印二维码
        try {
            recordService.printCode(record);
            return renderResult(Global.TRUE, text("二维码打印成功!"));
        } catch (Exception e) {
            return renderResult(Global.FALSE, e.getMessage());
        }
    }
}